﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Edu.Web.Biz;
using Edu.Web.Data.Entity;
using Edu.Web.Data.Service;

namespace Edu.Web.Controllers
{
    /// <summary>
    /// 考勤
    /// </summary>
    public class AttendanceController : BaseController
    {
        private readonly IClazzService _clazzService;
        private readonly IStudentService _studentService;
        private readonly IAttendanceService _attendanceService;

        public AttendanceController(IClazzService clazzService, IStudentService studentService, IAttendanceService attendanceService)
        {
            _clazzService = clazzService;
            _studentService = studentService;
            _attendanceService = attendanceService;
        }

        // GET: Attendance
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult List()
        {
            return View();
        }

        public ActionResult Create()
        {
            return View();
        }

        public ActionResult Detail(long groupId)
        {
            ViewBag.groupId = groupId;
            return View();
        }

        /// <summary>
        /// 获取班级列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SaveAttendance(List<AttendanceEntity> attendenceList)
        {
            try
            {
                if (Request.QueryString["clazzId"] == null)
                {
                    throw new Exception("请选择班级！");
                }
                long clazzId = Convert.ToInt64(Request.QueryString["clazzId"]);
                if (clazzId==0)
                {
                    throw new Exception("请选择班级！");
                }

                _attendanceService.AddList(attendenceList, CurrentUser.UserId, clazzId);
                return Content("true");
            }
            catch (Exception e)
            {
                return Content("err:" + e.Message);
            }
        }

        /// <summary>
        /// 获取考勤分组
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetAttendanceGroupList()
        {
            return Json(_attendanceService.GetGroupListByCreateUid(CurrentUser.UserId), JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 获取考勤分组详情
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetAttendanceDetail(long groupId)
        {
            var attendanceList = _attendanceService.GetAttendanceListByGroupId(groupId);
            var group = _attendanceService.GetAttendanceGroupById(groupId);
            dynamic dy = new ExpandoObject();
            dy.AttendanceList = attendanceList;
            dy.Group = group;
            return Json(dy, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 获取班级列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetAllClazzEntity()
        {
            return Json(_clazzService.LoadClazzByUserId(CurrentUser.UserId, 2), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 根据班级ID获取学生列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetStudentListByClazzId(long clazzId)
        {
            return Json(_studentService.LoadStudentListByClazzId(clazzId), JsonRequestBehavior.AllowGet);
        }
    }
}